package com.ebring.shareEducation.service.impl;

import com.ebring.shareEducation.bean.response.Talentrsg;
import com.ebring.shareEducation.bean.IndustryLable;
import com.ebring.shareEducation.bean.SkillsLable;
import com.ebring.shareEducation.bean.Talent;
import com.ebring.shareEducation.bean.UserAndSkillStatus;
import com.ebring.shareEducation.dao.IndustryLableMapper;
import com.ebring.shareEducation.dao.SkillsLableMapper;
import com.ebring.shareEducation.dao.TalentMapper;
import com.ebring.shareEducation.service.TalentService;
import com.ebring.shareEducation.utils.RspMsg;
import com.ebring.shareEducation.utils.UuidUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class TalentServiceImpl implements TalentService {

	@Autowired
	private TalentMapper talentMapper;
	@Autowired
	private SkillsLableMapper skillsLableMapper;
	@Autowired
	private IndustryLableMapper industryLableMapper;


	//查询所有行业
	@Override
	public RspMsg<IndustryLable> selectIndustry() {
		RspMsg rspMsg = new RspMsg();
		Map<String, Object> map = new LinkedHashMap<>();
		List<IndustryLable> industryLable = industryLableMapper.selectAll();
		if (industryLable.isEmpty() || industryLable == null) {
			rspMsg.setOK();
			rspMsg.setMsg("未找到相关数据");
			return rspMsg;
		}
		map.put("industryLable", industryLable);
		rspMsg.setOK();
		rspMsg.setResult(map);
		return rspMsg;
	}

	//查看所有的技能
	@Override
	public RspMsg<SkillsLable> selectSkills(String industryId) {
		RspMsg rspMsg = new RspMsg();
		Map<String, Object> map = new LinkedHashMap<>();
		List<SkillsLable> skillsLable = skillsLableMapper.selectSkills(industryId);
		if (skillsLable.isEmpty() || skillsLable == null) {
			rspMsg.setOK();
			rspMsg.setMsg("未找到相关数据");
			return rspMsg;
		}
		map.put("skillsLable", skillsLable);
		rspMsg.setOK();
		rspMsg.setResult(map);
		return rspMsg;
	}
	//新增加人才技能
	@Override
	public RspMsg<Talent> insertTalent(Talent talent) {
		RspMsg rspMsg = new RspMsg();
		talent.setTalentId(UuidUtils.getNumberId(10));
		talent.setState("0");
		int i = talentMapper.insertSelective(talent);
		if (i < 0) {
			rspMsg.setError();
			rspMsg.setMsg("插入失败");
			return rspMsg;
		}
		rspMsg.setOK();
		rspMsg.setMsg("插入成功");
		return rspMsg;
	}

	//查询所有的人才技能
	@Override
	public RspMsg<List<Talentrsg>> selectPhoneTalent(String talentId,String industryId,String talentType) {
		RspMsg rspMsg = new RspMsg();
		Map<String, Object> map = new LinkedHashMap<>();
		List<Talentrsg> talentList = talentMapper.selectPhoneTalent(talentId,industryId,talentType);
		if (talentList.isEmpty() || talentList == null) {
			rspMsg.setOK();
			rspMsg.setMsg("未找到相关数据");
			return rspMsg;
		}
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for (Talentrsg talentrsg : talentList) {
			try {
				if (talentrsg.getSupervipStarttime() != null && talentrsg.getSupervipEndtime() != null && talentrsg.getVipStarttime() != null && talentrsg.getVipEndtime() != null) {
					Date svst = sdf.parse(talentrsg.getSupervipStarttime());
					Date svet = sdf.parse(talentrsg.getSupervipEndtime());
					Date vst = sdf.parse(talentrsg.getVipStarttime());
					Date vet = sdf.parse(talentrsg.getVipEndtime());
					if (date.after(svst) && date.before(svet)) {
						talentrsg.setVipState("2");
					} else if (date.after(vst) && date.before(vet)) {
						talentrsg.setVipState("1");
					} else {
						talentrsg.setVipState("0");
					}
				} else if (talentrsg.getSupervipStarttime() != null && talentrsg.getSupervipEndtime() != null) {
					Date svst = sdf.parse(talentrsg.getSupervipStarttime());
					Date svet = sdf.parse(talentrsg.getSupervipEndtime());
					if (date.after(svst) && date.before(svet)) {
						talentrsg.setVipState("2");
					} else {
						talentrsg.setVipState("0");
					}
				} else if (talentrsg.getVipStarttime() != null && talentrsg.getVipEndtime() != null) {
					Date vst = sdf.parse(talentrsg.getVipStarttime());
					Date vet = sdf.parse(talentrsg.getVipEndtime());
					if (date.after(vst) && date.before(vet)) {
						talentrsg.setVipState("1");
					} else {
						talentrsg.setVipState("0");
					}
				} else {
					talentrsg.setVipState("0");
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		map.put("talentList", talentList);
		rspMsg.setOK();
		rspMsg.setResult(map);
		return rspMsg;
	}


	//查询所有的人才技能
	@Override
	public RspMsg<Talent> selectIdTalent(Talent talent) {
		RspMsg rspMsg = new RspMsg();
		Map<String, Object> map = new LinkedHashMap<>();
		List<Talent> talentList = talentMapper.selectIdTalent(talent);
		if (talentList.isEmpty() || talentList == null) {
			rspMsg.setError();
			rspMsg.setMsg("未找到相关数据");
			return rspMsg;
		}
		map.put("talentList", talentList);
		rspMsg.setOK();
		rspMsg.setResult(map);
		return rspMsg;
	}

	//查询所有的人才技能
	@Override
	public RspMsg<List<Talentrsg>> selectTalent(String talentId,String industryId,String talentType,String phone,String state) {
		RspMsg rspMsg = new RspMsg();
		Map<String, Object> map = new LinkedHashMap<>();
		List<Talentrsg> talentList = talentMapper.selectTalent(talentId,industryId,talentType,phone,state);
		if (talentList.isEmpty() || talentList == null) {
			rspMsg.setOK();
			rspMsg.setMsg("未找到相关数据");
			return rspMsg;
		}
		PageInfo<Talentrsg> pageInfo = new PageInfo<>(talentList);
		// 总条数
		map.put("total", pageInfo.getTotal());
		// 每页显示条数
		map.put("curPage", pageInfo.getPageSize());
		// 总页数
		map.put("pages", pageInfo.getPages());
		// 当前页
		map.put("pageNum", pageInfo.getPageNum());
		map.put("talentList", talentList);
		rspMsg.setOK();
		rspMsg.setResult(map);
		return rspMsg;
	}

	// 审核通过
	@Override
	@Transactional
	public RspMsg auditTalent(String talentId, String state) {
		RspMsg rspMsg = new RspMsg();
		Talent  talent =talentMapper.selectByPrimaryKey(talentId);
		talent.setState(state);
		int i = talentMapper.updateByPrimaryKeySelective(talent);
		if (i == 0) {
			rspMsg.setError();
			rspMsg.setMsg("更新信息失败");
			return rspMsg;
		}
		rspMsg.setOK();
		rspMsg.setMsg("更新信息成功");
		return rspMsg;
	}

	@Override
	public RspMsg personalInfoAndSkillsInfoStatus(String userId) {
		RspMsg rspMsg = new RspMsg();
		UserAndSkillStatus userAndTalentState = talentMapper.personalInfoAndSkillsInfoStatus(userId);
		if(userAndTalentState!=null){
			rspMsg.setOK();
			rspMsg.setResult(userAndTalentState);
		}else{
			rspMsg.setError();
			rspMsg.setMsg("查询不到数据");
		}
		return rspMsg;
	}
}
